package cn.gson.fengboot.aspect;


import cn.gson.fengboot.controller.DbusinessController;
import cn.gson.fengboot.model.dao.DoperatingrecordDao;
import cn.gson.fengboot.model.pojos.Doperatingrecord;
import com.alibaba.fastjson.JSON;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.Date;

@Aspect
@Component
public class SysLogAspect {
    private final static Logger log = org.slf4j.LoggerFactory.getLogger(SysLogAspect.class);


    @Autowired
    DbusinessController controller;
    @Autowired
    DoperatingrecordDao dao;

    //定义切点 @Pointcut
    //在注解的位置切入代码
    @Pointcut("@annotation(cn.gson.fengboot.aspect.MyLog)")
    public void logPoinCut(){

    }

    //切面 配置通知
    @AfterReturning("logPoinCut()")
    public void saveSysLog(JoinPoint joinPoint){
        log.info("======================日志开始记录=====================");
        //保存日志
        Doperatingrecord savelog = new Doperatingrecord();

        //从切面织入点处通过反射机制获取织入点处的方法
        MethodSignature signature =(MethodSignature) joinPoint.getSignature();
        //获取切入点所在的方法
        Method method =signature.getMethod();

        //获取操作
        MyLog myLog = method.getAnnotation(MyLog.class);
        if (myLog != null) {
            String modul = myLog.modul();
            String desc =myLog.desc();
            savelog.setDoName(desc);//详细描述
            savelog.setDoModul(modul);//模块名
        }

        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest requesturl = (HttpServletRequest) requestAttributes
                .resolveReference(RequestAttributes.REFERENCE_REQUEST);
        HttpSession session = requesturl.getSession();
        //获取请求的类名
        String className = joinPoint.getTarget().getClass().getName();
        //获取请求的方法名
        String methodName = method.getName();
        savelog.setDoClassmethod(className + "." + methodName);//方法名

        //请求的参数
        Object[] args = joinPoint.getArgs();
        //将参数所在的数组转换成json
        Object[] arguments  = new Object[args.length];
        for (int i = 0; i < args.length; i++) {
            if (args[i] instanceof ServletRequest || args[i] instanceof ServletResponse || args[i] instanceof MultipartFile) {
                continue;
            }
            arguments[i] = args[i];
        }
        String params = "";
        if (arguments != null) {
            try {
                params = JSON.toJSONString(arguments);
            } catch (Exception e) {
                params = arguments.toString();
            }
        }
        savelog.setDoParams(params);//参数列表

        Timestamp time1 = new Timestamp(new Date().getTime());
        savelog.setDoNoetime(time1);//操作人

        String doPeople = controller.getbuspeople(requesturl);
        if(doPeople!=null){
            savelog.setDoPeople(doPeople);//操作人
            savelog.setDoUrl(requesturl.getRequestURI());//请求路径
            dao.save(savelog);
        }else{

        }

    }



}
